home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16.lha / Python-1.6 / Lib / Python1.6 / test / test_userstring.py < prev    next >
Encoding:
Python Source  |  2000-04-03  |  6.5 KB  |  228 lines

  1. #!/usr/bin/env python
  2. import sys, string
  3. from test_support import verbose
  4. # UserString is a wrapper around the native builtin string type.
  5. # UserString instances should behave similar to builtin string objects.
  6. # The test cases were in part derived from 'test_string.py'.
  7. from UserString import UserString
  8.  
  9. if __name__ == "__main__":
  10.     verbose = 0
  11.  
  12. tested_methods = {}
  13.  
  14. def test(methodname, input, *args):
  15.     global tested_methods
  16.     tested_methods[methodname] = 1
  17.     if verbose:
  18.         print '%s.%s(%s) ' % (input, methodname, args),
  19.     u = UserString(input)
  20.     objects = [input, u, UserString(u)]
  21.     res = [""] * 3
  22.     for i in range(3):
  23.         object = objects[i]
  24.         try:
  25.             f = getattr(object, methodname)
  26.             res[i] = apply(f, args)
  27.         except:
  28.             res[i] = sys.exc_type
  29.     if res[0] != res[1]:
  30.         if verbose:
  31.             print 'no'
  32.         print `input`, f, `res[0]`, "<>", `res[1]`
  33.     else:
  34.         if verbose:
  35.             print 'yes'
  36.     if res[1] != res[2]:
  37.         if verbose:
  38.             print 'no'
  39.         print `input`, f, `res[1]`, "<>", `res[2]`
  40.     else:
  41.         if verbose:
  42.             print 'yes'
  43.  
  44. test('capitalize', ' hello ')
  45. test('capitalize', 'hello ')
  46.  
  47. test('center', 'foo', 0)
  48. test('center', 'foo', 3)
  49. test('center', 'foo', 16)
  50.  
  51. test('ljust', 'foo', 0)
  52. test('ljust', 'foo', 3)
  53. test('ljust', 'foo', 16)
  54.  
  55. test('rjust', 'foo', 0)
  56. test('rjust', 'foo', 3)
  57. test('rjust', 'foo', 16)
  58.  
  59. test('count', 'abcabcabc', 'abc')
  60. test('count', 'abcabcabc', 'abc', 1)
  61. test('count', 'abcabcabc', 'abc', -1)
  62. test('count', 'abcabcabc', 'abc', 7)
  63. test('count', 'abcabcabc', 'abc', 0, 3)
  64. test('count', 'abcabcabc', 'abc', 0, 333)
  65.  
  66. test('find', 'abcdefghiabc', 'abc')
  67. test('find', 'abcdefghiabc', 'abc', 1)
  68. test('find', 'abcdefghiabc', 'def', 4)
  69. test('rfind', 'abcdefghiabc', 'abc')
  70.  
  71. test('index', 'abcabcabc', 'abc')
  72. test('index', 'abcabcabc', 'abc', 1)
  73. test('index', 'abcabcabc', 'abc', -1)
  74. test('index', 'abcabcabc', 'abc', 7)
  75. test('index', 'abcabcabc', 'abc', 0, 3)
  76. test('index', 'abcabcabc', 'abc', 0, 333)
  77.  
  78. test('rindex', 'abcabcabc', 'abc')
  79. test('rindex', 'abcabcabc', 'abc', 1)
  80. test('rindex', 'abcabcabc', 'abc', -1)
  81. test('rindex', 'abcabcabc', 'abc', 7)
  82. test('rindex', 'abcabcabc', 'abc', 0, 3)
  83. test('rindex', 'abcabcabc', 'abc', 0, 333)
  84.  
  85.  
  86. test('lower', 'HeLLo')
  87. test('lower', 'hello')
  88. test('upper', 'HeLLo')
  89. test('upper', 'HELLO')
  90.  
  91. test('title', ' hello ')
  92. test('title', 'hello ')
  93. test('title', "fOrMaT thIs aS titLe String")
  94. test('title', "fOrMaT,thIs-aS*titLe;String")
  95. test('title', "getInt")
  96.  
  97. test('expandtabs', 'abc\rab\tdef\ng\thi')
  98. test('expandtabs', 'abc\rab\tdef\ng\thi', 8)
  99. test('expandtabs', 'abc\rab\tdef\ng\thi', 4)
  100. test('expandtabs', 'abc\r\nab\tdef\ng\thi', 4)
  101.  
  102. test('islower', 'a')
  103. test('islower', 'A')
  104. test('islower', '\n')
  105. test('islower', 'abc')
  106. test('islower', 'aBc')
  107. test('islower', 'abc\n')
  108.  
  109. test('isupper', 'a')
  110. test('isupper', 'A')
  111. test('isupper', '\n')
  112. test('isupper', 'ABC')
  113. test('isupper', 'AbC')
  114. test('isupper', 'ABC\n')
  115.  
  116. test('isdigit', '  0123456789')
  117. test('isdigit', '56789')
  118. test('isdigit', '567.89')
  119. test('isdigit', '0123456789abc')
  120.  
  121. test('isspace', '')
  122. test('isspace', ' ')
  123. test('isspace', ' \t')
  124. test('isspace', ' \t\f\n')
  125.  
  126. test('istitle', 'a')
  127. test('istitle', 'A')
  128. test('istitle', '\n')
  129. test('istitle', 'A Titlecased Line')
  130. test('istitle', 'A\nTitlecased Line')
  131. test('istitle', 'A Titlecased, Line')
  132. test('istitle', 'Not a capitalized String')
  133. test('istitle', 'Not\ta Titlecase String')
  134. test('istitle', 'Not--a Titlecase String')
  135.  
  136. test('splitlines', "abc\ndef\n\rghi")
  137. test('splitlines', "abc\ndef\n\r\nghi")
  138. test('splitlines', "abc\ndef\r\nghi")
  139. test('splitlines', "abc\ndef\r\nghi\n")
  140. test('splitlines', "abc\ndef\r\nghi\n\r")
  141. test('splitlines', "\nabc\ndef\r\nghi\n\r")
  142. test('splitlines', "\nabc\ndef\r\nghi\n\r")
  143. test('splitlines', "\nabc\ndef\r\nghi\n\r")
  144.  
  145. test('split', 'this is the split function')
  146. test('split', 'a|b|c|d', '|')
  147. test('split', 'a|b|c|d', '|', 2)
  148. test('split', 'a b c d', None, 1)
  149. test('split', 'a b c d', None, 2)
  150. test('split', 'a b c d', None, 3)
  151. test('split', 'a b c d', None, 4)
  152. test('split', 'a b c d', None, 0)
  153. test('split', 'a  b  c  d', None, 2)
  154. test('split', 'a b c d ')
  155.  
  156. # join now works with any sequence type
  157. class Sequence:
  158.     def __init__(self): self.seq = 'wxyz'
  159.     def __len__(self): return len(self.seq)
  160.     def __getitem__(self, i): return self.seq[i]
  161.  
  162. test('join', '', ('a', 'b', 'c', 'd'))
  163. test('join', '', Sequence())
  164. test('join', '', 7)
  165.  
  166. class BadSeq(Sequence):
  167.     def __init__(self): self.seq = [7, 'hello', 123L]
  168.  
  169. test('join', '', BadSeq())
  170.  
  171. test('strip', '   hello   ')
  172. test('lstrip', '   hello   ')
  173. test('rstrip', '   hello   ')
  174. test('strip', 'hello')
  175.  
  176. test('swapcase', 'HeLLo cOmpUteRs')
  177. transtable = string.maketrans("abc", "xyz")
  178. test('translate', 'xyzabcdef', transtable, 'def')
  179.  
  180. transtable = string.maketrans('a', 'A')
  181. test('translate', 'abc', transtable)
  182. test('translate', 'xyz', transtable)
  183.  
  184. test('replace', 'one!two!three!', '!', '@', 1)
  185. test('replace', 'one!two!three!', '!', '')
  186. test('replace', 'one!two!three!', '!', '@', 2)
  187. test('replace', 'one!two!three!', '!', '@', 3)
  188. test('replace', 'one!two!three!', '!', '@', 4)
  189. test('replace', 'one!two!three!', '!', '@', 0)
  190. test('replace', 'one!two!three!', '!', '@')
  191. test('replace', 'one!two!three!', 'x', '@')
  192. test('replace', 'one!two!three!', 'x', '@', 2)
  193.  
  194. test('startswith', 'hello', 'he')
  195. test('startswith', 'hello', 'hello')
  196. test('startswith', 'hello', 'hello world')
  197. test('startswith', 'hello', '')
  198. test('startswith', 'hello', 'ello')
  199. test('startswith', 'hello', 'ello', 1)
  200. test('startswith', 'hello', 'o', 4)
  201. test('startswith', 'hello', 'o', 5)
  202. test('startswith', 'hello', '', 5)
  203. test('startswith', 'hello', 'lo', 6)
  204. test('startswith', 'helloworld', 'lowo', 3)
  205. test('startswith', 'helloworld', 'lowo', 3, 7)
  206. test('startswith', 'helloworld', 'lowo', 3, 6)
  207.  
  208. test('endswith', 'hello', 'lo')
  209. test('endswith', 'hello', 'he')
  210. test('endswith', 'hello', '')
  211. test('endswith', 'hello', 'hello world')
  212. test('endswith', 'helloworld', 'worl')
  213. test('endswith', 'helloworld', 'worl', 3, 9)
  214. test('endswith', 'helloworld', 'world', 3, 12)
  215. test('endswith', 'helloworld', 'lowo', 1, 7)
  216. test('endswith', 'helloworld', 'lowo', 2, 7)
  217. test('endswith', 'helloworld', 'lowo', 3, 7)
  218. test('endswith', 'helloworld', 'lowo', 4, 7)
  219. test('endswith', 'helloworld', 'lowo', 3, 8)
  220. test('endswith', 'ab', 'ab', 0, 1)
  221. test('endswith', 'ab', 'ab', 0, 0)
  222.  
  223. # TODO: test cases for: int, long, float, complex, +, * and cmp
  224. s = ""
  225. for builtin_method in dir(s):
  226.     if not tested_methods.has_key(builtin_method):
  227.         print "no regression test case for method '"+builtin_method+"'"
  228.